﻿#region Version Info
/* ========================================================================
* 【功能概述】
* 
* 作者：Zhai Xianwen       时间：2012-3-21 9:35:42
* 邮箱 : zhaisky@163.com
* 
* 文件名：EventServer
* 版本：V1.0.1
*
* 修改者：           时间：              
* 修改说明：
* ========================================================================
*/
#endregion

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using EventClass;
using System.Collections;

namespace EventServer
{
    public class EventServer : MarshalByRefObject, IEventNotification
    {
        //htableSubscribed内部每项的Delegate链表中每一个委托都是透明代理
        private Hashtable htableSubscribed = new Hashtable(); //eventName -- Delegate(是一个链表)
        public EventServer()
        {
        }

        public override object InitializeLifetimeService()
        {
            //Remoting对象 无限生存期
            return null;
        }


        #region IEventNotification 成员
        //handler是一个透明代理，指向EventClient.OnRemoteEventHappen委托
        public void SubscribeEvent(string eventName, EventProcessHandler handler)
        {
            lock (this)
            {
                Delegate handlerList = (Delegate)this.htableSubscribed[eventName];

                if (handlerList == null)
                {
                    this.htableSubscribed.Add(eventName, handler);
                    return;
                }

                handlerList = Delegate.Combine(handlerList, handler);
                this.htableSubscribed[eventName] = handlerList;
            }
        }

        public void UnSubscribeEvent(string eventName, EventProcessHandler handler)
        {
            lock (this)
            {
                Delegate handlerList = (Delegate)this.htableSubscribed[eventName];

                if (handlerList != null)
                {
                    handlerList = Delegate.Remove(handlerList, handler);
                    this.htableSubscribed[eventName] = handlerList;
                }
            }
        }

        public void RaiseEvent(string eventName, object eventContent)
        {
            lock (this)
            {
                Delegate handlerList = (Delegate)this.htableSubscribed[eventName];
                if (handlerList == null)
                {
                    return;
                }

                object[] args = { eventName, eventContent };
                IEnumerator enumerator = handlerList.GetInvocationList().GetEnumerator();
                while (enumerator.MoveNext())
                {
                    Delegate handler = (Delegate)enumerator.Current;
                    try
                    {
                        handler.DynamicInvoke(args);
                    }
                    catch (Exception ee) //也可重试
                    {
                        ee = ee;
                        handlerList = Delegate.Remove(handlerList, handler);
                        this.htableSubscribed[eventName] = handlerList;
                    }
                }
            }
        }

        #endregion
    }

}
